package com.southwind.controller;

import com.southwind.entity.User;
import com.southwind.entity.UserVo;
import com.southwind.feign.UserFeign;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.websocket.server.PathParam;
import java.util.List;

/**
 * @program: aspringclouddemo
 * @description:
 * @author: xzf
 * @create: 2021-12-09 19:34
 **/
@Controller
@RequestMapping("/user")
public class UserClientHandler {
    @Resource
    private UserFeign userFeign;

    //   查询全部用户
    @GetMapping("/findAll")
    @ResponseBody
    public UserVo findAll(@RequestParam("page") Integer page, @RequestParam("limit") Integer limit) {
        System.out.println(page+"  "+limit);
        Integer index = (page - 1) * limit;
        return new UserVo(0,"", userFeign.count(), userFeign.findAll(index,limit));
    }

//    返回用户信息的页面
@GetMapping("/redirect/{localhost}")
public String view(@PathVariable("localhost") String localhost){
    return localhost;
}


    //    根据id查找用户
    @GetMapping("/findById/{id}")
    public User findById(@PathVariable("id") Integer id) {
        return userFeign.findById(id);
    }

    //    查询用户数量
    @GetMapping("/getUserCount")
    public Integer count() {
        return userFeign.count();
    }

    //    根据id删除用户数据
    @GetMapping("/deleteById/{id}")
    public String deleteById(@PathVariable("id") Integer id) {
        userFeign.deleteById(id);
        return "redirect:/user/redirect/user_manage";
    }

    //    添加用户信息
    @PostMapping("/save")  //如果是表单对象的提交不需要使用@RequestBody 否则将注入不进来
    public String addUser( User user) {  //因为这里使用的是微服务架构的模式 服务之间相互传递的数据是json格式的需要转化数据
        userFeign.save(user);
        return "redirect:/user/redirect/user_manage";

    }

    //    更新用户信息
    @PutMapping("/update")
    public void update(@RequestBody User user) {
        userFeign.update(user);
    }
}
